package xpn.platform.modules.workflow.other.usefulexpression;

import lombok.Getter;
import lombok.Setter;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import xpn.platform.common.persistence.AbstractBaseEntity;
import xpn.platform.modules.sys.user.SysUser;
import javax.persistence.*;
import javax.validation.constraints.NotNull;

/**
 * 个人常用语
 * @author duwei
 *
 */
@Entity
@Table(name = "wf_useful_expression")
@Getter
@Setter
public class UsefulExpression extends AbstractBaseEntity {

	private static final long serialVersionUID = -180442127213304686L;

	/**
	 * 所属用户
	 */
	@ManyToOne
	@JoinColumn(nullable = false)
	private SysUser user;

	/**
	 * 常用语内容
	 */
	@NotNull
	private String content;

	/**
	 * 排序号
	 */
	private Float sortNo;

	@PrePersist
	@PreUpdate
	public void onPreSave() {
		if (this.getSortNo() == null) {
			this.setSortNo(0f);
		}
		if (this.getUser() == null) {
			Subject subject = SecurityUtils.getSubject();
			SysUser user = (SysUser) subject.getPrincipals().getPrimaryPrincipal();
			if (user != null) {
				this.setUser(user);
			}
		}
	}
}
